package Reaction::InterfaceModel::ObjectClass;

use Reaction::ClassExporter;
use Reaction::Class;

class ObjectClass which {   
  override exports_for_package => sub {
    my ($self, $package) = @_;
    my %exports = $self->SUPER::exports_for_package($package);    
    
    $exports{proxies} = sub {
      my @attrs = @_;
      my $meta = $package->meta;
      foreach my $attr (@attrs) {
        my %opts = ( is => 'ro', clearer => "_clear_${attr}",
		     set_or_lazy_build($attr) );
	$meta->add_attribute( $attr, %opts);
	$meta->add_method( "build_${attr}",
			   sub{ shift->_object_model->$attr });	
      }
	
      my $clearer = sub{ $_[0]->$_ for map { "_clear_${_}" } @attrs };
      
      $meta->has_method('_clear_proxied') ?
	  $meta->add_before_method_modifier('_clear_proxied', $clearer):
	      	$meta->add_method('_clear_proxied', $clearer);
      
    };
    
    return %exports;
  };
  
};

1;

__END__;


=head1 NAME

Reaction::Class::InterfaceModel::ObjectClass

=head1 SYNOPSIS

    package MyApp::Model::Admin::Foo;
    use Reaction::Class::InterfaceModel::ObjectClass;
    use aliased 'Reaction::Class::InterfaceModel::Object';

    class Foo, is Object, which{
        proxies qw/id name description/;
	
	...
    };
    

=head1 DESCRIPTION

Extends C<Reaction::Class> to provide new sugar for InterfaceModel Objects.

=head1 Extended methods / new functionality

=head2 exports_for_package

Overridden to add exported methods C<proxies> and C<_clear_proxied>

=head2 proxies @attrs

Will proxy C<@attrs>, a list of attribute names from the instance's 
C<_object_model> to the local class as read-only attributes that will build lazily.

=head2 _clear_proxied

Will clear all proxied attributes.

=head2 AUTHORS

See L<Reaction::Class> for authors.

=head1 LICENSE

See L<Reaction::Class> for the license.

=cut
